package apple.encryptedNode.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import apple.encryptedNode.Constans;
import apple.encryptedNote.tools.DesUtils;
import apple.encryptedNote.tools.EncryptTool;

public class EncryptedNodeOpenHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 2;
    public static final String DATABASE_NAME = "EncryptedNotes";
    public static final String DICTIONARY_TABLE_NAME = "EncryptedNote";
	public static final String KEY_NAME = "fileName";
	public static final String KEY_CONTENT = "fileContent";//明文内容
	public static final String KEY_ENCRYPT_CONTENT = "encryptfileContent";//短信加解密后内容
	public static final String KEY_STATUS = "fileStatus";//短信加解密状态
	private static final String DICTIONARY_TABLE_CREATE = "CREATE TABLE "
		+ DICTIONARY_TABLE_NAME + " (" 
		+ "_id INTEGER primary key autoincrement, "
		+ KEY_NAME + " TEXT, " 
		+ KEY_STATUS + " TEXT, " //短信加解密状态 y加密，n解密状态
		+ KEY_ENCRYPT_CONTENT + " TEXT, "//密文
		+ KEY_CONTENT + " TEXT);";

	public EncryptedNodeOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		Log.d("EncryptedNodeOpenHelper", "sql: "+DICTIONARY_TABLE_CREATE);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		Log.d("EncryptedNodeOpenHelper", "sql: "+DICTIONARY_TABLE_CREATE);
		db.execSQL(DICTIONARY_TABLE_CREATE);
		//db.execSQL("insert into EncryptedNote values( 0,'apple','this is my first application');");
		db.execSQL("insert into EncryptedNote values( 0,'apple','n','','this is my first apple application');");
		
		try {
			String sql="insert into EncryptedNote values(33,'wp','y','"+DesUtils.encode(Constans.key, "this is my first wp application")+"','');";
			Log.d("EncryptedNodeOpenHelper", "sql: "+sql);
			db.execSQL(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d("EncryptedNodeOpenHelper", "onUpgrade");
        String  sql="drop table if exists "+ DICTIONARY_TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);

	}

}
